//
//  NSString+Length.m
//  神约
//
//  Created by qlwl on 17/1/22.
//  Copyright © 2017年 qlwl. All rights reserved.
//

#import "NSString+Length.h"

@implementation NSString (Length)
-(NSUInteger)textLength
{
    
    NSUInteger asciiLength = 0;
    
    for (NSUInteger i = 0; i < self.length; i++) {
        
        
        unichar uc = [self characterAtIndex: i];
        
        asciiLength += isascii(uc) ? 1 : 2;
    }
    
    NSUInteger unicodeLength = asciiLength;
    
    return unicodeLength;
}

- (NSString *)subStringToIndexByte:(NSUInteger)index
{
    NSUInteger asciiLength = 0;
    
    for (NSUInteger i = 0; i < self.length; i++) {
        unichar uc = [self characterAtIndex: i];
        
        asciiLength += isascii(uc) ? 1 : 2;
        if (index <= asciiLength) {
            return [self substringToIndex:i + 1];
        }
    }
    
    return self;
}
@end
